]> git.r.bdr.sh - rbdr/map/blame_incremental - Map/Presentation/Base Components/MapRender/MapGroup.swift
Bump build version
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapGroup.swift
... / ...
CommitLineData
1// Copyright (C) 2024 Rubén Beltrán del Río
2
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
15import ConcaveHull
16import SwiftUI
17
18struct MapGroup: View {
19
20 let mapSize: CGSize
21 let vertexSize: CGSize
22 let group: [Vertex]
23 let color: Color
24
25 let cornerSize = CGSize(width: 2.0, height: 2.0)
26 var strokeSize: CGFloat { 1.75 * vertexSize.width }
27
28 var hull: [CGPoint] {
29 let groupList = group.map({ vertex in
30 return [Double(vertex.position.x), Double(vertex.position.y)]
31 })
32 let hull = Hull()
33 let hullPoints = hull.hull(groupList, nil)
34 return hullPoints.compactMap({ object in
35 if let point = object as? [Double] {
36 return CGPoint(x: point[0], y: point[1])
37 }
38 return nil
39 })
40 }
41
42 var body: some View {
43 Path { path in
44 var initialMove: CGPoint?
45
46 for point in hull {
47 let offsetPoint = CGPoint(x: w(point.x), y: h(point.y))
48
49 if initialMove == nil {
50 path.move(to: offsetPoint)
51 initialMove = offsetPoint
52 } else {
53 path.addLine(to: offsetPoint)
54 }
55 }
56
57 if let initialMove = initialMove {
58 path.addLine(to: initialMove)
59 }
60
61 }
62 .applying(
63 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
64 )
65 .fill(color)
66 .stroke(
67 color,
68 style: StrokeStyle(
69 lineWidth: strokeSize,
70 lineCap: .round,
71 lineJoin: .round,
72 miterLimit: 0,
73 dash: [],
74 dashPhase: 0
75 )
76 )
77 }
78
79 func h(_ dimension: CGFloat) -> CGFloat {
80 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
81 }
82
83 func w(_ dimension: CGFloat) -> CGFloat {
84 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
85 }
86}
87
88#Preview {
89 MapGroup(
90 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
91 group: [
92 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
93 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
94 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
95 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
96 ], color: .red)
97}